package com.etc.dao.impl;

import com.etc.dao.CourseDao;
import com.etc.model.Course;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class CourseDaoImpl implements CourseDao {
    private SqlSessionFactory sqlSessionFactory;

    public CourseDaoImpl(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    public Integer CourseInsert(List<Course> courses) {
        SqlSession session = sqlSessionFactory.openSession();
        Integer result = null;
        for(Course course : courses){
            result = session.insert("CourseInsert", course);
            if (result == 1){
                System.out.println("成功添加数据"+course.toString());
            }
            session.commit();
        }
        return result;
    }

    public Course courseById(String cid) {
        SqlSession session = sqlSessionFactory.openSession();
        Course courseById = session.selectOne("courseById", cid);
        return courseById;
    }
}
